//
//  NDProgressHUD.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/9.
//

import SwiftUI
import SVProgressHUD

/// SVProgressHUD 的 SwiftUI 封装管理类
class NDProgressHUD {
    
    /// 初始化 HUD 配置
    static func configure() {
        SVProgressHUD.setDefaultStyle(.custom)
        SVProgressHUD.setDefaultMaskType(.custom)
        SVProgressHUD.setMinimumSize(CGSize(width: 120, height: 120))
        SVProgressHUD.setRingThickness(3.0)
        SVProgressHUD.setForegroundColor(.white)
        SVProgressHUD.setBackgroundColor(UIColor(white: 0, alpha: 0.8))
        SVProgressHUD.setFont(UIFont.systemFont(ofSize: 16))
        SVProgressHUD.setCornerRadius(12)
        SVProgressHUD.setMinimumDismissTimeInterval(1.5)
        SVProgressHUD.setMaximumDismissTimeInterval(5.0)
        SVProgressHUD.setErrorImage(UIImage())
        SVProgressHUD.setInfoImage(UIImage())
        SVProgressHUD.setSuccessImage(UIImage())
    }
    
    // MARK: - 显示方法
    
    /// 显示加载指示器
    static func show(_ status: String? = nil) {
        SVProgressHUD.show(withStatus: status)
    }
    
    /// 显示进度条
    static func showProgress(_ progress: Float, status: String? = nil) {
        SVProgressHUD.showProgress(progress, status: status)
    }
    
    /// 显示成功状态
    static func showSuccess(_ status: String? = nil) {
        SVProgressHUD.setImageViewSize(.zero)
        SVProgressHUD.showSuccess(withStatus: status)
    }
    
    /// 显示错误状态
    static func showError(_ status: String? = nil) {
        SVProgressHUD.setImageViewSize(.zero)
        SVProgressHUD.showError(withStatus: status)
    }
    
    /// 显示信息提示
    static func showInfo(_ status: String? = nil) {
        SVProgressHUD.setImageViewSize(.zero)
        SVProgressHUD.showInfo(withStatus: status)
    }
    
    // MARK: - 纯文字提示
    
    /// 显示纯文字提示（自动消失）
    static func showMessage(_ message: String) {
        // 设置图片尺寸为零以隐藏图片:cite[1]
        SVProgressHUD.setImageViewSize(.zero)
        // 显示一张空图片和状态文字:cite[1]
        SVProgressHUD.show(UIImage(), status: message)
        // 设置风格:cite[1]
        SVProgressHUD.setDefaultStyle(.dark)
        // 延迟消失:cite[1]
        SVProgressHUD.dismiss(withDelay: 2.0)
        
        // 恢复默认图片设置
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.1) {
            SVProgressHUD.setImageViewSize(CGSize(width: 28, height: 28))
            self.resetDefaultStyle()
        }
    }
    
    // MARK: - 隐藏方法
    
    /// 隐藏 HUD
    static func dismiss() {
        SVProgressHUD.dismiss()
    }
    
    /// 延迟隐藏
    static func dismiss(withDelay delay: TimeInterval) {
        SVProgressHUD.dismiss(withDelay: delay)
    }
    
    // MARK: - 私有方法
    
    private static func resetDefaultStyle() {
        SVProgressHUD.setDefaultStyle(.custom)
    }
}
